package com.sky.controller.user;

import com.sky.entity.Setmeal;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.result.Result;
import com.sky.service.impl.SetmealServiceImpl;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/setmeal")
public class SetmealController {

    @Autowired
    private SetmealServiceImpl setmealService;

    /**
     * 根据分类id查看套餐
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查看套餐")
    @Cacheable(cacheNames = "setmealCache", key = "#categoryId")  // key: setmealCache::10
    public Result<List<Setmeal>> list(@RequestParam Long categoryId) {
        log.info("当前查询套餐的分类id为:{}", categoryId);
        List<Setmeal> setmeals = setmealService.list(categoryId);
        if (setmeals.size() == 0) {
            throw new SetmealEnableFailedException("当前暂无套餐");
        }
        return Result.success(setmeals);
    }

    @GetMapping("/dish/{id}")
    @ApiOperation("根据套餐id查看包含的菜品")
    public Result<List<DishItemVO>> dishList(@PathVariable Long id) {
        log.info("当前要查的包含菜品的套餐id为:{}", id);
        List<DishItemVO> dishItemVOS = setmealService.dishList(id);
        if (dishItemVOS.size() == 0) {
            throw new SetmealEnableFailedException("当前暂无套餐");
        }
        return Result.success(dishItemVOS);
    }
}
